package com.example.day20_zhongji;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.day20_zhongji.bean.Cardban;

import java.util.ArrayList;
import java.util.List;

public class Sql extends SQLiteOpenHelper {
    private Context context;
    public Sql(Context context) {
        super(context, "card", null, 1);
        context=context;
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("create table card(id inteager primary key autoincrement,name varchar(20),dsec varchar(20))");
    }
    public void deletecard(String desc){
        Sql sql = new Sql(context);
        SQLiteDatabase writableDatabase = sql.getWritableDatabase();
        writableDatabase.delete("card","dsec",new String[]{desc+""});
        writableDatabase.close();
    }
    public void insertcard(String name,String desc){
        Sql sql = new Sql(context);
        SQLiteDatabase writableDatabase = sql.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name",name);
        values.put("dsec",desc);
        writableDatabase.insert("card",null,values);
        writableDatabase.close();
    }
    public List<Cardban> query(){
        Sql sql = new Sql(context);
        SQLiteDatabase writableDatabase = sql.getWritableDatabase();
        Cursor dsec = writableDatabase.query("dsec", null, null, null, null, null, null);
        ArrayList<Cardban> cardbans = new ArrayList<>();
        while (dsec.moveToNext()){
            int id = dsec.getInt(dsec.getColumnIndex("id"));
            String name = dsec.getString(dsec.getColumnIndex("name"));
            String dsec1 = dsec.getString(dsec.getColumnIndex("dsec"));
            cardbans.add(new Cardban(id,name,dsec1));
        }
        return cardbans;
    }
    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}
